﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using TA.Turbo.Design;



namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all control reference groups.
	/// </summary>
	[ParseChildren(true)]
	[PersistChildren(false)]
	public abstract class BaseControlReferenceGroup : BaseSpecialControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseControlReferenceGroup()
		{
			_postControls = new ControlCollection<ControlReference>(this);
			_returnControls = new ControlCollection<ControlReference>(this);
			_updatePanels = new MultiControlValue<UpdatePanel>(this);

			this.PostControlIds = "";
			this.ReturnControlIds = "";
			this.UpdatePanelIds = "";
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Comma-separated IDs of controls whose values will be posted.
		/// </summary>
		[Category("Transfer")]
		[DefaultValue("")]
		public string PostControlIds
		{
			get
			{
				return this._mpvPostControlIds.Value;
			}
			set
			{
				this._mpvPostControlIds.Value = value;
			}
		}
		/// <summary>
		/// Gets the collection of IDs of controls whose values will be posted.
		/// </summary>
		[Category("Transfer")]
		[Editor(typeof(ControlIdCollectionEditor<ValueControlIdLister>), typeof(System.Drawing.Design.UITypeEditor))]
		public List<string> PostControlIdList
		{
			get
			{
				return this._mpvPostControlIds.ValueList;
			}
		}
		/// <summary>
		/// Comma-separated IDs of controls whose values will be returned.
		/// </summary>
		[Category("Transfer")]
		[DefaultValue("")]
		public string ReturnControlIds
		{
			get
			{
				return this._mpvReturnControlIds.Value;
			}
			set
			{
				this._mpvReturnControlIds.Value = value;
			}
		}
		/// <summary>
		/// Gets the collection of IDs of controls whose values will be returned.
		/// </summary>
		[Category("Transfer")]
		[Editor(typeof(ControlIdCollectionEditor<ValueControlIdLister>), typeof(System.Drawing.Design.UITypeEditor))]
		public List<string> ReturnControlIdList
		{
			get
			{
				return this._mpvReturnControlIds.ValueList;
			}
		}
		/// <summary>
		/// List of controls whose values and/or properties will be posted.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Category("Transfer")]
		public ControlCollection<ControlReference> PostControls { get { return _postControls; } }
		/// <summary>
		/// List of controls whose values and/or properties will be returned.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Category("Transfer")]
		public ControlCollection<ControlReference> ReturnControls { get { return _returnControls; } }
		/// <summary>
		/// Comma-separated list of ids of UpdatePanels to be rendered.
		/// </summary>
		[Category("Transfer")]
		[DefaultValue("")]
		public string UpdatePanelIds
		{
			get
			{
				return this._updatePanels.Value;
			}
			set
			{
				this._updatePanels.Value = value;
			}
		}
		/// <summary>
		/// Gets the collection of IDs of <see cref="UpdatePanel"/>s which will be redrawn.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Category("Transfer")]
		[Editor(typeof(ControlIdCollectionEditor<UpdatePanelIdLister>), typeof(System.Drawing.Design.UITypeEditor))]
		public List<string> UpdatePanelIdList
		{
			get
			{
				return this._updatePanels.ValueList;
			}
		}
		/// <summary>
		/// Gets the collection of <see cref="UpdatePanel"/>s which will be redrawn.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public List<UpdatePanel> UpdatePanelList
		{
			get
			{
				return this._updatePanels.ControlList;
			}
		}
		#endregion
		#region METHODS
		/// <summary>
		/// Returns controls in PostControls and PostControlIds concatenated.
		/// </summary>
		/// <returns></returns>
		public IEnumerable<ControlReference> GetPostControls()
		{
			return GetControls(this._postControls, PostControlIds);
		}
		/// <summary>
		/// Returns controls in ReturnControls and ReturnControlIds concatenated.
		/// </summary>
		/// <returns></returns>
		public IEnumerable<ControlReference> GetReturnControls()
		{
			return GetControls(this._returnControls, ReturnControlIds);
		}

		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		ControlCollection<ControlReference> _postControls, _returnControls;
		MultiControlValue<UpdatePanel> _updatePanels;
		MultiPartValue _mpvPostControlIds = new MultiPartValue(),
			_mpvReturnControlIds = new MultiPartValue();
		#endregion
		#region METHODS
		/// <summary>
		/// Returns a collection of controls by concatenating the specified control collection and the controls referenced by the specified comma-separated list of control IDs.
		/// </summary>
		/// <param name="controls"></param>
		/// <param name="controlIds"></param>
		/// <returns></returns>
		private IEnumerable<ControlReference> GetControls(ControlCollection<ControlReference> controls, string controlIds)
		{
			List<ControlReference> res = new List<ControlReference>(controls);
			string[] ids = controlIds.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string id in ids)
			{
				res.Add(new ControlReference(this, id.Trim()));
			}
			return res;
		}
		#endregion
		#endregion
	}
}